package com.example.musicplayer.activity;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.CheckBox;
import android.text.method.PasswordTransformationMethod;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.example.musicplayer.R;
import com.example.musicplayer.bean.User;
import com.example.musicplayer.utils.Data;
import com.example.musicplayer.utils.GetAllActivity;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private Button btn_activity_login_login,btn_activity_login_register,btn_activity_login_help;
    private CheckBox checkBox;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        btn_activity_login_login=(Button)findViewById(R.id.btn_activity_login_login);
        btn_activity_login_login.setOnClickListener(new btuclock());
        btn_activity_login_help=(Button)findViewById(R.id.btn_activity_login_help);
        btn_activity_login_help.setOnClickListener(new btuclock1());
        btn_activity_login_register=(Button)findViewById(R.id.btn_activity_login_register);
        checkBox=(CheckBox) findViewById(R.id.check_box);
        btn_activity_login_register.setOnClickListener(new btucl());
        EditText txt3=(EditText)findViewById(R.id.et_activity_login_password);
        TextView time1 = (TextView) findViewById(R.id.tv_time1);
        TextView ew= (TextView) findViewById(R.id.text1);
        time1= (TextView) findViewById(R.id.tv_time1);
        ew.setOnClickListener(new btuclock1());
        txt3.setTransformationMethod(PasswordTransformationMethod.getInstance());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
//获取当前时间
        GetAllActivity.getInstance().addActivity(this);
        Date date = new Date(System.currentTimeMillis());
        time1.setText(simpleDateFormat.format(date));
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton mCompoundButton, boolean isChecked) {
                if(isChecked)
                {
                    txt3.setTransformationMethod(null);
                }else txt3.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });
    }

    class btuclock1 implements View.OnClickListener {
        public void onClick(View v) {
            Uri uri = Uri.parse("http://www.baidu.com");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);

        }
    }
    class btucl implements View.OnClickListener {
        public void onClick(View v) {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);


        }
    }
    class btuclock implements View.OnClickListener {
        public void onClick(View v) {

            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            EditText et_activity_login_username=(EditText)findViewById(R.id.et_activity_login_username);
            Bundle bundle=new Bundle();
            bundle.putString("username", et_activity_login_username.getText().toString());
            EditText et_activity_login_password=(EditText)findViewById(R.id.et_activity_login_password);
            Bundle bundle2=new Bundle();
            bundle2.putString("password", et_activity_login_password.getText().toString());
            intent.putExtras(bundle);
            intent.putExtras(bundle2);
            String url = getString(R.string.web_ip)+"/loginInfo";
            OkHttpClient okHttpClient = new OkHttpClient();
            MediaType JSON = MediaType.parse("application/json;charset=utf-8");
            JSONObject json = new JSONObject();
            try {
                json.put("username",et_activity_login_username.getText().toString());
                json.put("password", et_activity_login_password.getText().toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            RequestBody body = RequestBody.create(JSON,json.toJSONString()); // new

            final Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    Looper.prepare();
                    Toast.makeText(getApplicationContext(), "网络连接失败", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String json = response.body().string();
                    System.out.println(json);
                    if (json.equals("[]")){
                        Looper.prepare();
                        Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    }else{
                        JSONObject jsonObject=JSONObject.parseObject(json);
                        User user = jsonObject.toJavaObject(User.class);
                        final Data data = (Data)getApplication();
                        data.setUserID(user.getId());
                        System.out.println(user.getId());
                        startActivity(intent);
                        Looper.prepare();
                        Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    }

                }
            });

        }

    }


}


